package com.mo.star.controller;

import com.mo.star.common.BaseResponse;
import com.mo.star.common.ErrorCode;
import com.mo.star.common.ResultUtils;
import com.mo.star.service.MessageService;
import com.mo.star.service.impl.MessageServiceImpl;
import com.mo.star.vo.ChatCard;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Log4j
@RestController
@RequestMapping("/message")
@CrossOrigin(origins = {"http://localhost:5173"},allowCredentials = "true")
public class MessageController {
    @Resource
    private MessageServiceImpl messageService;
    @GetMapping("/chatList")
    public BaseResponse seChatList(HttpServletRequest request){
        List<ChatCard> cardList = messageService.seChatCart(request);
        if(cardList ==null)
            return ResultUtils.error(ErrorCode.NULL_ERROR);
        return ResultUtils.success(cardList);
    }
    @PostMapping("/messageList")
    public BaseResponse messageList(@RequestParam("fromUserId") String fromUser,@RequestParam("toUserId") String toUser){
        if(StringUtils.isAnyBlank(fromUser,toUser))
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(messageService.messageList(fromUser,toUser));
    }
}
